package matrices;

public class ComplexNumber implements MatrixElement{
	
	float real;
	float im;
	
	public ComplexNumber(float real, float im){
		this.real = real;
		this.im = im;
	}
	
	public MatrixElement add(MatrixElement b) {
		if(isCompatibleWith(b)){
			this.real += ((ComplexNumber)b).real;
			this.im += ((ComplexNumber)b).im;
		}
		return this;
	}
	
	public MatrixElement subtract(MatrixElement b) {
		if(isCompatibleWith(b)){
			this.real -= ((ComplexNumber)b).real;
			this.im -= ((ComplexNumber)b).im;
		}
		return this;
	}
	
	public MatrixElement multiply(MatrixElement element) {
		float a = this.real;
		float b = this.im;
		float c = ((ComplexNumber)element).real;
		float d = ((ComplexNumber)element).im;
		this.real = (a*c) - (b*d);
		this.im = (a*d) + (b*c);
		return this;
	}
	
	public MatrixElement createNewInstance() {
		return new ComplexNumber(this.real, this.im);
	}
	
	public MatrixElement zeroValue() {
		return new ComplexNumber(0 , 0);
	}
	
	public boolean isZeroValue() {
		return ( this.real == 0 && this.im == 0);
	}
	
	public MatrixElement unityValue() {
		return new ComplexNumber(1,0);
	}
	
	public boolean isUnityValue() {
		return (this.real == 1 && this.im == 0);
	}
	
	public boolean isEqualTo(MatrixElement b) {
		return ( this.real == ((ComplexNumber)b).real && this.im == ((ComplexNumber)b).im );
	}
	
	public boolean isCompatibleWith(MatrixElement b) {
		return (this.getClass() == b.getClass());
	}
	
	public float abs(){
		return (float) Math.hypot(real, im);
	}
	
	public float arg(){
		return (float) Math.atan(this.real/this.im);
	}
	
	public String toString(){
		String returnStr = (this.real + " ");
		returnStr += (this.im < 0)?this.im:("+"+this.im);
		return returnStr;
	}

	public MatrixElement invert() {
		float a = this.real;
		float b = this.im;
		this.real = a / (a*a + b*b);
		this.im = -b / (a*a + b*b);
		return this;
	}
	
}
